package database

import (
	"context"
	"fmt"
	"gitee.com/sdzxq/api_server/conf"
	"github.com/mongodb/mongo-go-driver/mongo"
	"github.com/mongodb/mongo-go-driver/mongo/options"
)


var DbClient *mongo.Client
var err error
var m = map[string]string{"SERVICE_NAME": "mongodb"}
var cd = options.Credential {
	AuthMechanism:           "SCRAM-SHA-1",
	AuthMechanismProperties: m,
	AuthSource:              "mainzxq",
	Username:                "mainzxq",
	Password:                "mainzxq",
}

func init() {
	// 注意格式, 新版本mongo需要使用SCRAM-SHA-1的验证方式，因此需要做上面一堆blablabla的事情
	var dbOption options.ClientOptions
	var uString = fmt.Sprintf("mongodb://%s:%s@%s:%s",conf.DB_username, conf.DB_pwd, conf.DB_host, conf.DB_port)
	DbClient, err = mongo.Connect(context.TODO(),uString, dbOption.SetAuth(cd) )
	if err != nil {
		fmt.Println("database got an error!")
		return
	}
}